package com.syncomponents.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.springframework.aop.support.AopUtils;

public class ReflectionUtils extends org.springframework.util.ReflectionUtils {

    /**
     * it is not effectually if use proxy,
     * Attempt to find a {@link Method} on the supplied class with the supplied name
     * and no parameters. Searches all superclasses up to {@code Object}.
     * <p>
     * Returns {@code null} if no {@link Method} can be found.
     * 
     * 
     * 
     * @param clazz the class to introspect
     * @param annoClazz the annotation class of the method
     * @return the Method Array, or {@code null} if none found
     */
    public static <T extends Annotation> Method[] findMethod(Object obj, final Class<T> annoClazz) {
        if (obj == null || annoClazz == null) {
            return null;
        }
        List<Method> annoMethods = new ArrayList<Method>();

        Method[] methods;
        if (AopUtils.isAopProxy(obj)) {
            methods = AopUtils.getTargetClass(obj).getDeclaredMethods();
        } else {
            methods = obj.getClass().getDeclaredMethods();
        }
        if (methods != null) {
            for (Method method : methods) {
                if (method.isAnnotationPresent(annoClazz)) {
                    annoMethods.add(method);
                }
            }
        }
        return annoMethods.toArray(new Method[annoMethods.size()]);
    }
}
